package com.dongway.netcondition.socketToBox;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import com.dongway.netcondition.model.DevInfo;

public class GetDevIsListeren {



	public static DevInfo getDevIsListeren(Integer dev_id) throws IOException {

		 DevInfo devInfo = new DevInfo();
		devInfo.setId(dev_id);

		Socket socket = SocketContent.getInstance().getSocket(dev_id);
		// Map<String, String> map = new HashMap<String, String>();
		// InputStream in = null;
		String hex = "";

		byte[] b1 = { 0x61, 0x31, 0x30, 0x2c, (byte) 0x88, (byte) 0x87, 0x05,
				0x06, 0x02, 0x00, 0x00, 0x00 };

		hex = Integer.toHexString(dev_id);
		hex = hex.length() > 2 ? hex : ("0" + hex);
		byte[] b2 = HexStringToBytes.hexStringToBytes(hex);
		b1[10] = b2[0];

		try {
			OutputStream out = socket.getOutputStream();
			out.write(b1);
			out.flush();
		} catch (IOException e) {
			System.out.println("查询失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		}
		SocketInfo info = SocketContent.getInfo(dev_id);
		while (info.getIpListener() == null) {
			System.out.println("查询中。。。。。。");
			// 为空，一直等！
			try {
				Thread.sleep(100);
			} catch (InterruptedException ige) {
			}
		}
		// 根据ID获取字节信息
		byte[] b = info.getIpListener();

		// out.write(b1);
		// out.flush();
		// //接收信息
		// in = socket.getInputStream();
		// byte[] b = new byte[1024];
		// int len = in.read(b);
		// //接受的信息进行预处理
		// while(len!=48)
		// {
		// out.write(b1);
		// out.flush();
		//
		// in = socket.getInputStream();
		// b = new byte[1024];
		// len = in.read(b);
		// }

		// b[12]是检测时间间隔 单位秒 b[16]为连续检测的次数

		devInfo.setCheckIntervalTime((int) b[12]);
		devInfo.setCheckTimes((int) b[16]);

		switch (b[36]) {

		case 0:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(0);
			break;
		case 1:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(0);
			break;

		case 2:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(0);
			break;

		case 3:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(0);
			break;

		case 4:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(0);
			break;

		case 5:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(0);
			break;

		case 6:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(0);
			break;

		case 7:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(0);
			break;

		case 8:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(1);
			break;

		case 9:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(1);
			break;

		case 10:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(1);
			break;

		case 11:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(0);
			devInfo.setVpnLocalIpState(1);
			break;

		case 12:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(1);
			break;

		case 13:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(0);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(1);
			break;

		case 14:
			devInfo.setPnServerIpState(0);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(1);
			break;

		case 15:
			devInfo.setPnServerIpState(1);
			devInfo.setPnLocalIpState(1);
			devInfo.setVpnServerIpState(1);
			devInfo.setVpnLocalIpState(1);
			break;

		default:
			break;
		}
      System.out.println(devInfo);
		return devInfo;

	}

}
